package service;

import dao.ActorAssRoleDao;
import domin.Actor;
import domin.ActorAssRole;
import util.JdbcHelper;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
public final class ActorAssRoleService {
    private static final ActorAssRoleDao actorAssRoleDao = ActorAssRoleDao.getInstance();
    private static final ActorAssRoleService actorAssRoleService = new service.ActorAssRoleService();
    private ActorAssRoleService() {
    }
    public static ActorAssRoleService getInstance() {
        return actorAssRoleService;
    }
    public Collection<ActorAssRole> findAll(Actor actor) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<ActorAssRole> actorAssRoles=null;
        connection.setAutoCommit(false);
        try {
            actorAssRoles = actorAssRoleDao.findAll(connection, actor);
            connection.commit();
        }
        catch (SQLException e) {
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return actorAssRoles;
    }

    public Collection<ActorAssRole> findAll() throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        System.out.println("到这里了");
        Collection<ActorAssRole> actorAssRoles = null;
        try {
            actorAssRoles = actorAssRoleDao.findAll(connection);
            connection.commit();
        }
        catch (SQLException e) {
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return actorAssRoles;
    }

    public ActorAssRole find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        ActorAssRole actorAssRole = null;
        try {
            actorAssRole = actorAssRoleDao.find(connection, id);
            connection.commit();
        }
        catch (SQLException e) {
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return actorAssRole;

    }

    public boolean update(ActorAssRole actorAssRole) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean updated = false;
        try {
            updated = actorAssRoleDao.update(connection, actorAssRole);
            connection.commit();
        }
        catch (SQLException e) {
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean delete(Integer id) throws SQLException {
        boolean deleted = false;
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        try {
            deleted = actorAssRoleDao.delete(connection, id);
            connection.commit();
        }
        catch (SQLException e) {
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}

